package org.example.controller;

import org.example.model.cart.ShoppingCart;
import org.example.model.common.dtos.ResponseResult;
import org.example.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 购物车
 */
@CrossOrigin
@RestController
@RequestMapping("/shopping-cart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 新增购物车数据并返回购物车数据id
     * @param shoppingCart
     * @return
     */
    @PostMapping("/shopping-cart")
    public ResponseResult AddShoppingCart(@RequestBody ShoppingCart shoppingCart) {
        return ResponseResult.okResult(shoppingCartService.InsertShoppingCart(shoppingCart));
    }

    /**
     * 根据id获取购物车数据
     */
    @GetMapping("/shopping-cart/{id}")
    public ResponseResult GetShoppingCartById(@PathVariable int id) {
        return ResponseResult.okResult(shoppingCartService.SelectShoppingCartById(id));
    }

    /**
     * 通过token中的用户id获取对应的购物车列表
     */
    @GetMapping("/cart-list")
    public ResponseResult GetShoppingCartsByUseId() {
        return ResponseResult.okResult(shoppingCartService.SelectShoppingCartsByUseId());
    }

    /**
     * 根据id修改对应购物车数据
     */
    @PutMapping("/shopping-cart")
    public ResponseResult UpdateShoppingCartById(@RequestBody ShoppingCart shoppingCart) {
        return ResponseResult.okResult(shoppingCartService.UpdateShoppingCartById(shoppingCart));
    }

    /**
     * 根据id列表批量删除购物车数据
     */
    @DeleteMapping("/cart-list")
    public ResponseResult DeleteShoppingCartByIds(@RequestBody int[] ids) {
        return ResponseResult.okResult(shoppingCartService.DeleteShoppingCartByIds(ids));
    }
}
